#!/usr/bin/python3
# -*- coding: utf-8; tab-width: 4; indent-tabs-mode: t -*-

import os
import subprocess
from gi.repository import GLib
from usr_session_common import UsrSessionHelperBase


class UsrSessionHelperAdvancedCli(UsrSessionHelperBase):

    """
    notes:
        * sub-modules should use XDG_SEAT environment variables to know which seat we are on
    """

    def __init__(self, parent, session_id, **kwargs):
        super().__init__(parent, session_id)
        self._kmsconProc = None

    def start(self, machine):
        command = ["pipewire"]
        subprocess.Popen(command)

        command = ["pipewire-pulse"]
        subprocess.Popen(command)

        command = ["wireplumber"]
        subprocess.Popen(command)

        command = ["kmscon"]
        self._kmsconProc = subprocess.Popen(command)
        GLib.child_watch_add(self._kmsconProc.pid, self._callback)

    def exit(self):
        if self._kmsconProc is not None:
            self._kmsconProc.terminate()
            self._kmsconProc.wait()

    def _callback(self, pid, status):
        GLib.spawn_check_wait_status(status)
